<?php

namespace App\Http\Controllers\Screen;

use App\Http\Controllers\Controller;
use App\Models\Bookcase;
use App\Models\BookcaseExhibition;
use App\Models\Exhibition;
use App\Models\ExhibitionProduction;
use App\Models\Production;
use App\Models\ExhibitionVideo;
use App\Models\Video;
use Illuminate\Support\Facades\Validator;

/**
 * 展览屏幕基本信息
 */
class IndexController extends CommonController
{


    /**
     * 获取基本信息
     * @param cab_code  机柜编号
     */
    public function getCaseInfo()
    {
        $validator = Validator::make(
            $this->request->all(),
            ['cab_code' => 'required|size:9'],
            ['cab_code.required' => '柜子编号不能为空', 'cab_code.size' => '柜子编号不符合要求']
        );
        if ($validator->fails()) {
            return $this->returnApi(201, $validator->errors()->first()); //获取第一条错误返回
        }
        $bookcase_id = Bookcase::getBoocaseIdByCabCode($this->request->cab_code);
        if (empty($bookcase_id)) {
            return  $this->returnApi(202, '编号有误');
        }
        //获取机柜对应的所有的展览
        $exhibitionModelObj = new Exhibition();
        $bookcaseExhibitionModelObj = new BookcaseExhibition();
        $exhibitionProductionModelObj = new ExhibitionProduction();
        $productionModelObj = new Production();
        $exhibitionVideoModelObj = new ExhibitionVideo();
        $videoModelObj = new Video();
        $exhibition_ids = $bookcaseExhibitionModelObj->getExhibitionIdByBookcaseId($bookcase_id);
        if (empty($exhibition_ids)) {
            return $this->returnApi(202, '此展示屏未设置展览，请联系管理员处理');
        }
        $data = [];
        $i = 0;
        foreach ($exhibition_ids as $key => $val) {
            //获取展览信息及前言
            $exhibition_info = $exhibitionModelObj
                ->select('id', 'name', 'fore_id', 'roll_speed', 'page_stay_min_time','template')
                ->with(['foreword' => function ($query) {
                    $query->select('id', 'name', 'font_color', 'intro', 'intro_en', 'subhead', 'template', 'roll_speed', 'page_stay_min_time', 'img', 'img_thumb','template')->where('is_del', 1);
                }])
                ->where('is_del', 1)
                ->where('status', 1)
                ->where('start_time', '<', date('Y-m-d H:i:s'))
                ->where('end_time', '>', date('Y-m-d H:i:s'))
                ->find($val);

            if (empty($exhibition_info)) {
                continue;
            }
            //获取作品信息id
            $production_ids = $exhibitionProductionModelObj->getProductionIdByExhibitionId($exhibition_info->id);
            //获取所有的作品信息
            if ($production_ids) {
                $production_info = $productionModelObj->getProduction($production_ids)->toArray();
            } else {
                $production_info = [];
            }

            $data[$i] = $exhibition_info->toArray();
            $data[$i]['production_info'] = $production_info;

            //获取视频信息id
            $video_ids = $exhibitionVideoModelObj->getVideoIdByExhibitionId($exhibition_info->id);
            //获取所有的作品信息
            if ($video_ids) {
                $video_info = $videoModelObj->getVideo($video_ids)->toArray();
            } else {
                $video_info = [];
            }
            $data[$i]['video_info'] = $video_info;

            $i++;
        }

        if ($data) {
            $result = $exhibitionModelObj->exhibitionData($data);
            return $this->returnApi(200, '获取成功', true, $result); //获取第一条错误返回
        }
        return $this->returnApi(203, '暂无数据');
    }
}
